package com.example.mybatisdemo.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPSClient;

import java.io.File;

@Slf4j
public class FTPUtil {

    public static void ensureParentDirectoryExists(FTPSClient ftpClient, String remotePath) {
        try {
            // 获取上级目录
            String parentDir = new File(remotePath).getParent();
            if (parentDir != null) {
                // 切换到上级目录
                log.info("进入ftp目录：{}", parentDir);
                ftpClient.changeWorkingDirectory(parentDir);
                String[] dirs = parentDir.split("/");
                String workingDir = "/";
                for (String dir : dirs) {
                    if (StringUtils.isNotBlank(dir)) {
                        workingDir += dir + "/";
                        // 尝试进入目录
                        boolean enterFlag = ftpClient.changeWorkingDirectory(workingDir);
                        log.info("进入ftp目录：{}", parentDir);
                        if (!enterFlag) {
                            // 如果目录不存在，则创建目录
                            boolean createFlag = ftpClient.makeDirectory(workingDir);
                            if (createFlag) {
                                log.info("创建目录成功：{}", workingDir);
                            } else {
                                log.info("创建目录失败：{}", dir);
                            }

                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("创建ftp路径失败", e);
        }
    }


    public static void ensureParentDirectoryExists(FTPClient ftpClient, String remotePath) {
        try {
            // 获取上级目录
            String parentDir = new File(remotePath).getParent();
            if (parentDir != null) {
                // 切换到上级目录
                log.info("进入ftp目录：{}", parentDir);
                ftpClient.changeWorkingDirectory(parentDir);
                String[] dirs = parentDir.split("/");
                String workingDir = "/";
                for (String dir : dirs) {
                    if (StringUtils.isNotBlank(dir)) {
                        workingDir += dir + "/";
                        // 尝试进入目录
                        boolean enterFlag = ftpClient.changeWorkingDirectory(workingDir);
                        log.info("进入ftp目录：{}", parentDir);
                        if (!enterFlag) {
                            // 如果目录不存在，则创建目录
                            boolean createFlag = ftpClient.makeDirectory(workingDir);
                            if (createFlag) {
                                log.info("创建目录成功：{}", workingDir);
                            } else {
                                log.info("创建目录失败：{}", dir);
                            }

                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("创建ftp路径失败", e);
        }
    }
}
